package com.atguigu.dga.governance.assessor;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.atguigu.dga.governance.bean.AssessParam;
import com.atguigu.dga.governance.bean.GovernanceAssessDetail;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.text.ParseException;
import java.util.Date;

@Component("NO_ACCESS")
public class NoAccessAssessor extends Assessor{
    @Override
    public void checkProblem(AssessParam assessParam, GovernanceAssessDetail governanceAssessDetail) throws ParseException {

        //1 取得最后访问时间
        Date lastAccessTime = assessParam.getTableMetaInfo().getTableLastAccessTime();

        //2 计算最后访问时间和当前时间的时间差
        String assessDate = assessParam.getAssessDate();
        Date currentDate = DateUtils.parseDate(assessDate, "yyyy-MM-dd");

        long curMs = currentDate.getTime();
        long lastAccessTimeMs = lastAccessTime.getTime();
        long diffMs= curMs- lastAccessTimeMs;
        long diffDays = diffMs / 1000 / 3600 / 24;

        //3 时间差换算成天数，用天数和参数天数比较 如果大于等于则给0分
        //获取参数中的天数
        String metricParamsJson = assessParam.getGovernanceMetric().getMetricParamsJson();
        JSONObject paramJsonObj = JSON.parseObject(metricParamsJson);
        Long paramDays = paramJsonObj.getLong("days");
        if(diffDays>=paramDays){
            governanceAssessDetail.setAssessScore(BigDecimal.ZERO);
            governanceAssessDetail.setAssessProblem("表"+diffDays+"天未被访问");
        }
        governanceAssessDetail.setAssessComment("距离当前访问天数："+diffDays);

    }
}
